/*
 * Copyright (c) 2011-2012 Alexander Dubu
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * o  Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * o  Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * o  Neither the name Axil nor the names of its contributors may be used to
 *    endorse or promote products derived from this software without specific
 *    prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package axil.stdlib.money.function;

import axil.api.AxilObject;
import axil.api.extend.Environment;
import axil.api.extend.Function;
import axil.api.extend.Parameters;
import axil.api.extend.Source;
import axil.stdlib.core.type.Nil;
import axil.stdlib.money.type.Currency;
import axil.stdlib.money.type.CurrencyConverter;
import axil.stdlib.money.type.Money;
import axil.stdlib.time.type.Date;

import java.util.TimeZone;

import static axil.framework.Functions.*;


/**
 * Convert the given monetary value to the given currency. The general form of
 * this function is:
 *
 * 		convert(from, to, then, now, using)
 *
 * This uses the native currency conversion mechanisms integral to the Money
 * object type.
 */
public class Convert implements Function {
	/**
	 * Execute this function with the given arguments. The Axil compiler will
	 * ensure that the number of arguments passed to the function are valid.
	 * If this function fails during execution for any reason, then an exception
	 * is thrown.
	 */
	public AxilObject eval(Environment env, Parameters params, Source source) {
		TimeZone tz = env.persona().timezone();
		Money money = cast(Money.class, params.at(0));
		Currency to = Currency.from(str(params.at(1)));
		Date then = cast(Date.class, params.at(2));
		Date now = cast(Date.class, params.at(3));
		AxilObject using = params.at(4);
		if (using == Nil.object) {
			return money.convert(to, then.date(tz), now.date(tz));
		}
		CurrencyConverter c = Money.converter(str(using));
		if (c == null) {
			throw error(axil(), "unknown-converter",
			            nv("converter", str(using)));
		}
		return money.convert(to, then.date(tz), now.date(tz), c);
	}
}
